package org.rapan;

import java.util.Set;

import javax.naming.InitialContext;
import javax.naming.NamingException;

import org.rapan.EJB.ReportInt;
import org.rapan.jpa.Consultant;
import org.rapan.jpa.Report;

import com.vaadin.ui.Table;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;

@SuppressWarnings("serial")
public class ReportsWindow extends Window {
	Table reportsTable = new Table();

	public ReportsWindow(Consultant c) {
		setModal(true);
		setResizable(false);
		setDraggable(false);
		setWidth("800px");
		VerticalLayout componentLayout = new VerticalLayout();
		componentLayout.setSpacing(true);
		componentLayout.setMargin(true);
		setContent(componentLayout);
		componentLayout.addComponent(reportsTable);
		reportsTable.setSizeFull();
		setTable();
		addItemsToTable(c);
	}

	private void setTable() {
		reportsTable.addContainerProperty("Company Name", String.class, null);

		reportsTable.addContainerProperty("Company Email", String.class, null);
		reportsTable
				.addContainerProperty("Consultant Name", String.class, null);
		reportsTable.addContainerProperty("Consultant Email", String.class,
				null);
		reportsTable.addContainerProperty("Start Date", String.class, null);
		reportsTable.addContainerProperty("Hours Per Day", String.class, null);
		reportsTable.addContainerProperty("End Date", String.class, null);

	}

	private void addItemsToTable(Consultant c) {
		try {
			InitialContext context = new InitialContext();
			ReportInt repInt = (ReportInt) context
					.lookup("Consult/ReportBean/local");

			Set<Report> repSet = repInt.getAllReportsForConsultant(c);
			for (Report rep : repSet) {
				Object[] objects = { rep.getCompany().getName(),
						rep.getCompany().getEmail(),
						rep.getConsultant().getFullName(),
						rep.getConsultant().getEmail(),
						rep.getStarting_date().toString(),
						Integer.toString(rep.getHours_per_day()),
						rep.getEnd_date().toString() };
				reportsTable.addItem(objects, rep.getId());
			}
		} catch (NamingException e) {
		}
	}
}
